<template>
  <PageWrapper :contentFullHeight="true">
    <BasicTable @register="registerTable">
      <template #toolbar>
        <Authority :value="addMenu">
          <AButton type="primary" @click="handleCreate"> 新增菜单</AButton>
        </Authority>
        <Authority :value="delMenu">
          <AButton type="danger" color="error" @click="handleDelMore"> 删除菜单</AButton>
        </Authority>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              onClick: handleEdit.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>

    <MenuModel @register="registerModal" @success="handleSuccess"/>
  </PageWrapper>
</template>

<script lang="ts">
import { defineComponent } from "vue";
import { BasicTable, TableAction, useTable } from "/@/components/Table";
import { columns } from "/@/views/menu/menu.data";
import { getMenuList } from "/@/api/sys/menu";
import PageWrapper from "/@/components/Page/src/PageWrapper.vue";
import Authority from "/@/components/Authority/src/Authority.vue";
import { useModal } from "/@/components/Modal";
import MenuModel from "/@/views/menu/MenuModel.vue";


export default defineComponent({
  name: "MenuPage",
  components: { MenuModel, Authority, PageWrapper, BasicTable, TableAction },
  setup() {

    const btnPre = {
      addMenu: "addMenu",
      edtMenu: "edtMenu",
      delMenu: "delMenu"
    };
    const [registerModal, { openModal }] = useModal();
    const [registerTable, { reload, getSelectRows,getSelectRowKeys }] = useTable({
      title: "菜单列表",
      api: getMenuList,
      columns,
      beforeFetch: (params) => {
        console.log(params);
        return params;
      },
      rowSelection: { type: "checkbox" },
      pagination: true,
      striped: true,
      useSearchForm: false,
      showTableSetting: true,
      bordered: false,
      canResize:false,
      showIndexColumn: false,
      actionColumn: {
        width: 80,
        title: "操作",
        dataIndex: "action",
        slots: { customRender: "action" },
        fixed: undefined
      }
    });

    function handleCreate() {
      openModal(true, {
        isUpdate: false
      });
    }

    function handleEdit(record: Recordable) {
      console.log(record);
    }

    function handleDelete(record: Recordable) {
      console.log(record);
    }

    function handleSuccess() {
      reload();
    }

    function handleDelMore() {
      const rowKeys = getSelectRowKeys()
      const row = getSelectRows()

      console.log(rowKeys);
      console.log(row);
    }

    return {
      registerTable,
      handleEdit,
      handleDelete,
      handleSuccess,
      handleDelMore,
      handleCreate,
      registerModal,
      ...btnPre
    };
  }
});
</script>

<style scoped>

</style>
